/**
 * @Author:  renyc
 * @Date: 2023年2月23日13:17:33
 */

'use strict';
import ChartLayer from "../../chart/ChartLayer";
import ChartType from "../../chart/ChartType";
import LayerType from "../LayerType";
import {LayerEventType, SourceEventType} from "../../event/EventType";

class NewHeatmapLayer extends ChartLayer{
    
    constructor(name, options){
        if(!options){
            throw new Error('HeatmapLayer：options无效')
        }
        if(!options.source){
            throw new Error('HeatmapLayer：source无效')
        }

        super(name, options);

        this._type = LayerType.CHART;
        this._chartType = ChartType.HEATMAP;

        
        /**
         * 热力图上点的value字段，系统从Source中读取该字段的值。
         */
        this._valueField = options.valueField;
        //注册source的Event事件
        this._source.on(SourceEventType.FETCHED, this._onSourceFetched, this);
    }

    /**
     * 将Layer挂载到Viewer上
     * 由子类实现该函数
     * @private
     * @Override 
     * @param {Viewer} viewer 
     */
    _attach(){

        this._initECharts();

        if(this._viewer&&this._source){
            if(this._source._delegate){
                //source已经获取数据，直接将数据添加到Viewer上
                this._viewer.dataSources.add(this._source._delegate);
            }
            else{
                //source尚未获取数据，则source从数据源获取数据后，在attach到viewer上
                this._source.fetch();
            }
        }
    }

    /**
     * 将Layer从Viewer上卸载
     * 由子类实现该函数
     * @private
     * @Override 
     */
    _detach(){
        this._delegate.then(dataSource=>{            
            dataSource.entities.removeAll();
        })
    }

    /**
     * Source获取数据后的Event响应函数
     */
    _onSourceFetched(evt){
        //注释掉添加实体的代码
        //this._viewer.dataSources.add(evt.data);

        //1. 渲染Heatmap
        this.render();

        //2. 触发Layer数据加载完成(LOADED)事件
        this._fire(LayerEventType.LOADED, {
            target : this
        });
    }

    /**
     * 渲染heatmap
     */
    render(){
        let json = this._source.json;
        let points = [];

        //构造ECharts Heatmap数据结构
        json.features.forEach(feature => {
            if(feature){
                if(feature.geometry){
                    if(feature.geometry.type=="Point"){
                        let value = undefined;
                        let coordinates = feature.geometry.coordinates;
                        if(this._valueField){
                            value = feature.properties[this._valueField] || 1;
                        }
                        else{
                            value = 1
                        }
                        points.push([coordinates[0],coordinates[1],value]);
                    }
                }
            }
        });
        
        let echartOption = {
			animation: false,
			GLMap: {},
			visualMap: {
			  show: false,
			  top: 'top',
			  min: 0,
			  max: 5,
			  seriesIndex: 0,
			  calculable: true,
			  inRange: {
				color: ['blue', 'blue', 'green', 'yellow', 'red']
			  }
			},
			series: [
			  {
				type: 'heatmap',
				coordinateSystem: 'GLMap',
				data: points,
				pointSize: 5,
				blurSize: 6
			  }
			]
		};

        this._echarts.setOption(echartOption);
    }
}

export default NewHeatmapLayer
